import React, {Component} from 'react';
import {Button} from 'antd';
import {config} from "../config";
import {user} from "../value";
import app from '../AppMethod';


class Index extends Component {
    constructor(props) {
        super(props);
        this.state = {}
    }

    render() {
        return (
            <div style={{paddingTop: '50px'}}>
                <div>
                    <div style={{textAlign: 'center', marginTop: '100px'}}>
                        <Button style={{width: '96%', height: '45px', fontSize: '18px'}} type='primary'
                                onClick={() => this.handleDrop1()}>开始配送</Button>
                    </div>
                    <div style={{textAlign: 'center', marginTop: '60px'}}>
                        <Button style={{width: '96%', height: '45px', fontSize: '18px'}} type='primary'
                                onClick={() => this.handleDrop3()}>配送记录</Button>
                    </div>
                    <div style={{textAlign: 'center', marginTop: '60px'}}>
                        <Button style={{width: '96%', height: '45px', fontSize: '18px'}} type='primary'
                                onClick={() => this.handleDrop2('/searchOrderDetail')}>查询订单</Button>
                    </div>
                </div>
            </div>
        )
    };

    /**
     * 处理开始配送按钮事件 判断用户是否是配送负责人，
     * 如果是则判断是否有未配送订单，如果有则直接跳转到配送列表页，如果否则跳转到扫码页
     * @returns {Promise.<void>}
     */
    async handleDrop1() {
        let _this = this;
        let employee_id = app.getData('employee_id');
        let resultIfDuty = await app.post(user.QUERY_IFISDUTYSEND, {employee_id: employee_id});
        if (resultIfDuty.code === 1) {
            let is_duty = resultIfDuty.data.is_duty;
            if (is_duty == 0) {
                app.alert('您不是配送负责人', 'error', 3);
                return;
            }
        } else {
            app.alert(resultIfDuty.msg, 'error', 3);
            return;
        }
        let resultIfNotFinish = await app.post(user.QUERY_IFHAVEUNFINISH, {employee_id: employee_id});
        if (resultIfNotFinish.code === 1) {
            let is_have = resultIfNotFinish.data.is_have;
            if (is_have == 0) {
                _this.props.history.push(config.path + '/addOrder');
            } else if (is_have == 1) {
                alert('您有未完成配送的订单');
                _this.props.history.push(config.path + '/deliveryOrder');
            }
        } else {
            app.alert(resultIfNotFinish.msg, 'error', 3);
            return;
        }
    }

    /**
     * 处理跳转页面事件
     * @param path
     */
    handleDrop2(path) {
        this.props.history.push(config.path + path);
    };

    /**
     * 处理查看配送记录按钮事件
     * @returns {Promise.<void>}
     */
    async handleDrop3() {
        let _this = this;
        let employee_id = app.getData('employee_id');
        let resultIfDuty = await app.post(user.QUERY_IFISDUTYSEND, {employee_id: employee_id});
        if (resultIfDuty.code === 1) {
            let is_duty = resultIfDuty.data.is_duty;
            if (is_duty == 0) {
                app.alert('您不是配送负责人', 'error', 3);
                return;
            }
        } else {
            app.alert(resultIfDuty.msg, 'error', 3);
            return;
        }
        _this.props.history.push(config.path + '/finishOrder');
    }
}

export default Index;